#include "common.h"
#include "stdio.h"
#include "port.h"
#include "delay.h"
__AppBlInfo gAppBlInfo;

uint8_t GuTable_ASCII[16] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
                             0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46};

__attribute__((section("ram_code")))

void app_init(void)
{
	gAppBlInfo.systick_cntL = 0;
	gAppBlInfo.systick_cntH = 0;
	eMBInit(0x1,115200,0);
	eMBEnable();
	printf("system init finish\r\n");
}

void app_1mstimer(void)
{
	gAppBlInfo.systick_cntL ++;
	if (gAppBlInfo.systick_cntL > 1000)
	{
		gAppBlInfo.systick_cntL = 0;
		gAppBlInfo.systick_cntH++;
	}
}

__attribute__((section("ram_code")))
void app_10mstimer(void)
{
}

__attribute__((section("ram_code")))
void app_100mstimer(void)
{
}

__attribute__((section("ram_code")))
void app_1stimer(void)
{
	printf("system is running, tick = %u\r\n",gAppBlInfo.systick_cntH);
}

uint16_t mystrcpy(char* dest,const char* src)
{
		assert(dest != NULL);
		assert(src!= NULL);
	  uint16_t i = 0;
	  while(*src != '\0')
		{	
			*dest++ = *src++;
			i++;
		}
		return i;
}

/*__attribute__((section("ram_code")))*/ 
void HardFault_Handler_C(unsigned int* hardfault_args)
{
		delay_ms(50);
    printf("Crash, dump regs:\r\n");
    printf("PC    = 0x%08X\r\n",hardfault_args[6]);
    printf("LR    = 0x%08X\r\n",hardfault_args[5]);
#if 0
    printf("R0    = 0x%08X\r\n",hardfault_args[0]);
    printf("R1    = 0x%08X\r\n",hardfault_args[1]);
    printf("R2    = 0x%08X\r\n",hardfault_args[2]);
    printf("R3    = 0x%08X\r\n",hardfault_args[3]);
    printf("R12   = 0x%08X\r\n",hardfault_args[4]);
    printf("PSR   = 0x%08X\r\n",hardfault_args[7]);
    printf("BFAR  = 0x%08X\r\n",*(unsigned int*)0xE000ED38);
    printf("CFSR  = 0x%08X\r\n",*(unsigned int*)0xE000ED28);
    printf("HFSR  = 0x%08X\r\n",*(unsigned int*)0xE000ED2C);
    printf("DFSR  = 0x%08X\r\n",*(unsigned int*)0xE000ED30);
    printf("AFSR  = 0x%08X\r\n",*(unsigned int*)0xE000ED3C);
    printf("SHCSR = 0x%08X\r\n",SCB->SHCSR);
    printf("dump sp stack[sp sp-512]:\r\n");
    uint16_t i = 0;
    do
    {
        printf("0x%08X,",*(hardfault_args++));
        i++;
        if(i%4 == 0)
        printf("\r\n");
    }
    while(i<128);
#endif
    NVIC_SystemReset();
    while(1);
}

